package com.wsl.sharding.configuration;

import com.wsl.sharding.Utils.DbShareUtils;
import com.wsl.sharding.pojo.DBDb;
import com.wsl.sharding.pojo.DBGroup;
import com.wsl.sharding.pojo.DbTable;
import com.wsl.sharding.repository.DBDbRepository;
import com.wsl.sharding.repository.DBGroupRepository;
import com.wsl.sharding.repository.DbTableRepository;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.dao.DataAccessException;
import org.springframework.data.redis.connection.RedisConnection;
import org.springframework.data.redis.core.RedisCallback;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;

import java.util.*;

/**
 * @Author:wangshuliang
 * @Description:
 * @Date:Created in 8:08 2019/5/15
 * @Modified by:
 */
@Component
public class DbPrepare implements CommandLineRunner {
    Logger logger = LoggerFactory.getLogger(DbPrepare.class);
    @Autowired
    private DbTableRepository dbTableRepository;
    @Autowired
    private DBGroupRepository dbGroupRepository;
    @Autowired
    private DBDbRepository dbDbRepository;
    @Autowired
    private RedisTemplate<Object,Object> redisTemplate;



    @Override
    public void run(String... strings) throws Exception {
        //缓存表结构表
        List<DbTable> list_Table = (List<DbTable>) dbTableRepository.findAll();

        redisTemplate.opsForValue().set("db_table",list_Table);
        //缓存组结构表
        List<DBGroup> list_Group = (List<DBGroup>) dbGroupRepository.findAll();
        redisTemplate.opsForValue().set("db_group",list_Group);
        //缓存数据库表
        List<DBDb> list_Db = (List<DBDb>) dbDbRepository.findAll();
        redisTemplate.opsForValue().set("db_db",list_Db);
        logger.info("分表数据缓存完成");
        DbShareUtils.dbDbList=list_Db;
        DbShareUtils.dbGroupList=list_Group;
        DbShareUtils.dbTableList=list_Table;
    }
}
